package com.dense.kuiniu.bookborrow.business.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.business.entity.BorrowBookInfo;
import com.dense.kuiniu.bookborrow.utils.RandomCoverUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @author Administrator
 * @date 2020/8/11
 */
public class BookRenewResultAdapter extends BaseQuickAdapter<BorrowBookInfo, BaseViewHolder> {


    public BookRenewResultAdapter(int layoutResId, @Nullable List<BorrowBookInfo> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, BorrowBookInfo borrowBookBean) {
        ImageView cover = baseViewHolder.getView(R.id.iv_cover);
        Glide.with(getContext())
                .load(borrowBookBean.getBookCover())
                .centerCrop()
                .error(RandomCoverUtils.getRandomCover())
                .placeholder(RandomCoverUtils.getRandomCover())
                .into(cover);
        boolean isSucceed = "Y".equals(borrowBookBean.getBackFlag());
        if (isSucceed) {
            String[] resultInfo = borrowBookBean.getBackInfor().split("应还时间为：");
            if (resultInfo.length == 2) {
                baseViewHolder.setText(R.id.tv_return_time_value, resultInfo[1]);
            }
        } else {
            baseViewHolder.setText(R.id.tv_return_time_value, "-----");
        }

        baseViewHolder.setTextColor(R.id.tv_name, isSucceed ? Color.parseColor("#4D81FF") :
                Color.parseColor("#BFBFBF"));
        baseViewHolder.setText(R.id.tv_borrow_time_value, isSucceed ? borrowBookBean.getBorrowDate() :
                "-----");
        if (isSucceed) {
            baseViewHolder.setBackgroundResource(R.id.iv_borrow_failed, R.mipmap.ic_renew_succeed);
        } else {
            baseViewHolder.setBackgroundResource(R.id.iv_borrow_failed, R.mipmap.ic_renew_failed);
        }

        if (isSucceed) {
            baseViewHolder.setText(R.id.tv_error_info, "");
        } else {
            baseViewHolder.setText(R.id.tv_error_info, borrowBookBean.getBackInfor());
        }
        baseViewHolder.setText(R.id.tv_name, String.format("《%s》", borrowBookBean.getBookName()));
        baseViewHolder.setText(R.id.tv_bar_code_value, borrowBookBean.getBarCode());
        ImageView tag = baseViewHolder.getView(R.id.iv_tag_periodical);
        tag.setVisibility(View.GONE);
    }


}